package com.project2.controller;

import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.po.MarketTopic;
import com.project2.bean.vo.CreateTopicVo;
import com.project2.bean.vo.ReadTopicVo;
import com.project2.service.AdminTopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 专题模块
 * @author Frank Zhu
 */
@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {
    @Autowired
    AdminTopicService topicService;

    /**
     * 专题列表显示
     * @author Frank Zhu
     * @param page
     * @param title
     * @param subtitle
     * @return
     */
    @RequiresPermissions("admin:topic:list")
    @RequestMapping("list")
    public ResultVo getAllTopics(PageBo page, String title, String subtitle){
        CommonData<MarketTopic> allTopics = topicService.getAllTopics(page, title, subtitle);
        return ResultVo.ok(allTopics);
    }

    /**
     * 新增专题，三个参数需要我们自己添加，addTime、updateTime、deleted
     * 为了返回的参数，专门设置了CreteTopicVo
     * 创建时不写阅读量，响应会是null，但是最终前端显示默认会是1k
     * @param newTopic
     * @return
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:topic:create")
    @RequestMapping("create")
    public ResultVo createTopic(@RequestBody MarketTopic newTopic){
        CreateTopicVo topic = topicService.createTopic(newTopic);
        return ResultVo.ok(topic);
    }

    /**
     * 编辑专题信息时候的信息回显，除了topic的基本信息外
     * 需要返回一个专题相关的所有商品信息，只是部分信息
     * @param id
     * @return
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:topic:read")
    @RequestMapping("read")
    public ResultVo getSpecifiedTopicInfo(Integer id) {
        ReadTopicVo topicInfoVo = topicService.getSpecifiedTopicInfo(id);
        return ResultVo.ok(topicInfoVo);
    }

    /**
     * 根据最新信息进行id选中的选择性更新
     * @param updatedTopic
     * @return
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:topic:update")
    @RequestMapping("update")
    public ResultVo updateTopicInfo(@RequestBody MarketTopic updatedTopic){
        MarketTopic uptodateTopic = topicService.updateTopicInfo(updatedTopic);
        return ResultVo.ok(uptodateTopic);
    }

    /**
     * 逻辑删除，更新updateTime和deleted
     * @param deleteInfo
     * @return
     * @author Frank Zhu
     */
    @RequiresPermissions("admin:topic:delete")
    @RequestMapping("delete")
    public ResultVo deleteTopic(@RequestBody MarketTopic deleteInfo) {
        topicService.deleteTopic(deleteInfo);
        return ResultVo.ok();
    }

    /**
     * 批量删除
     * @param map
     * @return
     */
    @RequiresPermissions("admin:topic:batch-delete")
    @RequestMapping("batch-delete")
    public ResultVo batchDelete(@RequestBody  Map map){
        List<Integer> ids = (List<Integer>) map.get("ids");
        topicService.batchDelete(ids);
        return ResultVo.ok();
    }
}
